const express = require('express');
const cors = require('cors');

const elements = [{ name: "zs", age: 20, car: "bmw" }]; // 将单个元素放入数组中
const app = express();
app.use(cors());
app.use(express.json()); // 用于解析 JSON 请求体

app.get('/api', (req, res) => {
    const ageQueryParam = parseInt(req.query.age); // 确保 age 参数可以转换为整数
    if (isNaN(ageQueryParam)) {
        return res.status(400).send('无效的 age 参数');
    }
    
    const foundElement = elements.find(element => element.age === ageQueryParam);
    if (foundElement) {
        if (!foundElement.car) {
            foundElement.car = "我没有车，但是我把有车";
        }
        res.json(foundElement);
    } else {
        res.status(404).send('未找到元素');
    }
});

app.listen(30001, () => {
    console.log('Example app listening on port 30001!');
});
